<script lang="ts" setup>
const props = defineProps(["path"]);
type menuList = { text: string; link?: string; children?: menuList[] };
const menuList = ref([
  { text: "首页", link: "/" },
  { text: "产品与能力", link: "/product_ability" },
  {
    text: "解决方案",
    children: [
      { text: "解决方案-智慧餐饮", link: "/solution/catering" },
      { text: "解决方案-零售", link: "/solution/retail" },
      { text: "解决方案-支付", link: "/solution/payment" },
    ],
  },
  { text: "开发者中心", link: "/developer" },
  { text: "帮助中心", link: "/help" },
]);
const active = (item: menuList): boolean => {
  if (item.link) {
    return props.path === item.link;
  } else if (item.children) {
    return item.children.some((i) => props.path === i.link);
  }
  return false;
};
const isLogin = computed(() => userStore().getToken());
const userName = computed(() => userStore().userName);
const swiperIndex = computed(() => appStore().swiperIndex);
const showLogin = computed(() => !isLogin.value);
const logout = () => {
  ElMessageBox.confirm("确认退出登录吗?", "提示", {
    type: "warning",
  }).then(() => {
    userStore().removeToken();
    location.href = "/";
  });
};
const goLogin = () => {
  if (swiperIndex.value != 0 && props.path == "/") {
    location.href = "/";
    return;
  }
  navigateTo("/");
};
// 是否显示弹窗
const showSelectMerchant = ref(false);
</script>

<template>
  <!-- 导航栏 -->
  <div class="nav is-sticky">
    <div class="content">
      <div class="left">
        <div class="logo">
          <NuxtLink to="/"
            ><img src="/images/layouts/logo.png" alt=""
          /></NuxtLink>
        </div>
      </div>
      <div class="right">
        <ul>
          <li
            :class="{ active: active(item) }"
            v-for="(item, index) in menuList"
            :key="index"
          >
            <NuxtLink class="link" :to="item.link">{{ item.text }}</NuxtLink>
            <div class="li-content" v-if="item.text === '解决方案'">
              <div
                class="li-item"
                v-for="(child, childIndex) in item.children"
                :key="childIndex"
              >
                <NuxtLink class="li-a" :to="child.link">
                  <img
                    class="fa"
                    :src="'/images/layouts/fa' + childIndex + '.png'"
                    alt="fa1.png"
                  />
                  <span>{{ child.text }}</span>
                  <img
                    class="arrow"
                    src="/images/layouts/arrow-red.png"
                    alt="arrow-red.png"
                  />
                </NuxtLink>
              </div>
            </div>
          </li>
        </ul>
        <div class="user-head" v-if="isLogin">
          <img
            class="user-img"
            src="/images/layouts/user-img.png"
            alt="user-img"
          />
          <img class="more" src="/images/layouts/more.png" alt="more" />
          <div class="user-content">
            <div class="user-h">
              <img
                class="users"
                src="/images/layouts/uses.png"
                alt="uses.png"
              />
              <div class="user-top">
                <p>{{ userName }}</p>
                <p class="con" @click="showSelectMerchant = true">
                  <span class="con-span">商户中心</span>
                  <span class="con-span">服务商中心</span>
                </p>
                <div class="logout" @click="logout">
                  <span>退出登录</span>
                  <img
                    class="arrow"
                    src="/images/layouts/arrow-red.png"
                    alt="arrow-red.png"
                  />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="login-btn" v-if="showLogin" @click="goLogin">立即登录</div>
      </div>
    </div>
  </div>

  <client-only>
    <Teleport to="body">
      <SelectMerchant v-model="showSelectMerchant" />
    </Teleport>
  </client-only>
</template>

<style lang="scss" scoped>
.nav {
  height: 88px;
  background-color: #ffffff;
  border-top: 6px solid #e12010;
  border-bottom: 1px solid #cbcbcb;
  box-sizing: border-box;
  z-index: 3;
  width: 100%;
  &.is-sticky {
    position: sticky;
    top: 0;
    left: 0;
  }

  .content {
    padding: 0 30px;
    box-sizing: border-box;
  }

  .right {
    display: flex;
    align-items: center;
  }

  .login-btn {
    font-weight: 400;
    font-size: 15px;
    color: #ffffff;
    width: 88px;
    height: 48px;
    background: #e22000;
    border-radius: 4px 4px 4px 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    &:hover {
      background: rgba(226, 32, 0, 0.8);
    }
  }

  .logo {
    width: 170px;
    height: 80px;
    img {
      width: 100%;
      height: 100%;
    }
  }

  ul {
    display: block;
    margin-right: 40px;
    &::after {
      content: "";
      display: block;
      clear: both;
    }
  }

  li {
    cursor: pointer;
    float: left;
    margin-left: 52px;
    text-align: center;
    position: relative;
    &:first-child {
      margin-left: 0;
    }

    .link {
      display: block;
      font-weight: 400;
      font-size: 18px;
      color: #070a14;
      line-height: 81px;
    }

    &.active {
      &::after {
        content: "";
        display: block;
        width: 54px;
        height: 6px;
        background: #e12010;
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -27px;
      }
    }

    &:hover {
      .li-content {
        display: block;
      }
    }

    .li-content {
      width: 236px;
      border-radius: 4px;
      padding: 30px 20px;
      box-sizing: border-box;
      background: #ffffff;
      box-shadow: 0px 10px 30px 0px rgba(0, 24, 74, 0.1318);
      position: absolute;
      z-index: 4;
      left: 25px;
      display: none;
      &::before {
        content: "";
        display: block;
        width: 0;
        height: 0;
        border-left: 7px solid transparent;
        border-right: 7px solid transparent;
        border-bottom: 7px solid #ffffff;
        position: absolute;
        top: -7px;
        left: 25px;
      }
      .li-item {
        margin-bottom: 20px;
        &:hover {
          .arrow {
            transform: translateX(10px);
          }
        }
        &:last-child {
          margin-bottom: 0;
        }
      }
      .li-a {
        display: flex;
        align-items: center;
      }
      .fa {
        width: 24px;
        height: 24px;
      }
      span {
        margin-left: 20px;
        display: inline-block;
        font-weight: 400;
        font-size: 14px;
        color: #000000;
      }
      .arrow {
        transition: transform 0.3s ease-in-out;
        margin-left: auto;
        width: 10px;
        height: 6px;
      }
    }
  }

  .user-head {
    display: flex;
    align-items: center;
    position: relative;
    height: 100%;
    height: 81px;
    cursor: pointer;
    &:hover {
      .user-content {
        display: block;
      }
    }
    .user-img {
      width: 40px;
      height: 40px;
    }
    .more {
      width: 9px;
      height: 7px;
      margin-left: 20px;
    }
    .user-content {
      width: 236px;
      border-radius: 4px;
      padding: 30px 20px;
      box-sizing: border-box;
      background: #ffffff;
      box-shadow: 0px 10px 30px 0px rgba(0, 24, 74, 0.1318);
      position: absolute;
      z-index: 11;
      left: -180px;
      top: 80px;
      display: none;
      .user-h {
        display: flex;
      }
      &::before {
        content: "";
        display: block;
        width: 0;
        height: 0;
        border-left: 7px solid transparent;
        border-right: 7px solid transparent;
        border-bottom: 7px solid #ffffff;
        position: absolute;
        top: -7px;
        right: 25px;
      }
      .users {
        width: 24px;
        height: 24px;
      }
      .user-top {
        margin: 0 auto;
        min-width: 112px;
        .con {
          display: flex;
          align-items: center;
          margin-top: 15px;
          .con-span {
            font-weight: 400;
            font-size: 12px;
            color: #b97300;
            display: inline-block;
            position: relative;
            &:first-child {
              margin-right: 15px;
              &::after {
                position: absolute;
                right: -8px;
                top: 50%;
                transform: translateY(-50%);
                content: "";
                display: block;
                width: 1px;
                height: 14px;
                border: 1px solid rgba(85, 85, 85, 0.2);
              }
            }
          }
        }
        p {
          font-weight: 400;
          font-size: 14px;
          color: #000000;
        }
        .logout {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-top: 10px;
          span {
            font-weight: 400;
            font-size: 14px;
            color: #e12010;
          }
          .arrow {
            transition: transform 0.3s ease-in-out;
          }
          &:hover {
            .arrow {
              transform: translateX(10px);
            }
          }
        }
      }
    }
  }
}
</style>
